﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Windsor;
using M2B.GestionEntretiens.Contracts;
using M2B.GestionEntretiens.Helpers.JSON;
using M2B.GestionEntretiens.Models;
using M2B.GestionEntretiens.POCOS;
using Newtonsoft.Json;

namespace M2B.GestionEntretiens.Controllers
{
    public class TestsController : Controller
    {
        #region Properties
        /// <summary>
        /// Type des tests
        /// </summary>
        public List<Referentiel> TypeTests
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "TYPE_TEST").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        /// Evaluation des Tests
        /// </summary>
        public List<Referentiel> EvaluationTests
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "EVALUATION_TEST").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        ///Durée des tests
        /// </summary>
        public List<Referentiel> DureeTests
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "DUREE_TEST").ToList();
                }
                return new List<Referentiel>();
            }
        }
        /// <summary>
        /// Test Model
        /// </summary>
        public TestsModel TestsModel
        {
            get
            {

                var testsModel = Session["TestsModel"] as TestsModel;
                if (testsModel == null)
                {
                    var container = Session["Container"] as IWindsorContainer;
                    if (container != null) testsModel = new TestsModel(container.Resolve<ITestsService>());
                    Session["TestsModel"] = testsModel;
                }
                return testsModel;

            }
            private set { Session["testsModel"] = value; }
        }
        #endregion
        //
        // GET: /Tests/

        public ActionResult Index()
        {
            ViewData["TypeTests"] = TypeTests;
            ViewData["DureeTests"] = DureeTests;
            return View();
        }
        /// <summary>
        /// Chargement de la liste des tests
        /// </summary>
        /// <returns>Tests</returns>
        public ActionResult LoadTests()
        {
            string strTests = JsonConvert.SerializeObject(TestsModel.Tests,
               new EFNavigationPropertyConverter());
            return Json(strTests, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Chargement de la liste des tests
        /// </summary>
        /// <returns>Tests</returns>
        public ActionResult SearchTest(string textSearch,string typeTest)
        {
            string strTests = JsonConvert.SerializeObject(TestsModel.Tests.Where(el=>el.Type.ToString()==typeTest&&(el.Libelle.ToUpper().Contains(textSearch.ToUpper()))),
               new EFNavigationPropertyConverter());
            return Json(strTests, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// Modification d'un test
        /// </summary>
        /// <param name="testId">Id</param>
        /// <returns></returns>
        public ActionResult EditTest(int testId)
        {

            ViewData["TypeTests"] = TypeTests;
            ViewData["EvaluationTests"] = EvaluationTests;
            ViewData["DureeTests"] = DureeTests;

            var test = this.TestsModel.GetTest(testId);
            return PartialView(test);
        }
        /// <summary>
        /// Ajouter un test
        /// </summary>
        /// <returns></returns>
        public ActionResult AddTest()
        {
            ViewData["TypeTests"] = TypeTests;
            ViewData["EvaluationTests"] = EvaluationTests;
            ViewData["DureeTests"] = DureeTests;

            return PartialView(new Test() { Type = 1, Evaluation = 1 });
        }
        /// <summary>
        /// Ajouter un nouveau test
        /// </summary>
        /// <param name="test">Test</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateTest(Test test)
        {
            try
            {
                TestsModel.CreateTest(test);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }
        /// <summary>
        /// Supprimer un test
        /// </summary>
        /// <param name="testId">Test</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteTest(int testId)
        {
            try
            {
                TestsModel.DeleteTest(testId);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        /// <summary>
        /// Chargement de la liste des questions d'un test à modifier
        /// </summary>
        /// <param name="testId">Id Test</param>
        /// <returns></returns>
        public ActionResult LoadQuestions(int testId)
        {
            var test = TestsModel.Tests.FirstOrDefault(el => el.TestId == testId);

            if (test != null)
            {
                var questions = test.TestQuestions.Select(el => el.Question).ToList();
                string strQuestions = JsonConvert.SerializeObject(questions,
              new EFNavigationPropertyConverter());
                return Json(strQuestions, JsonRequestBehavior.AllowGet);
            }

            return Json(new List<Question>(), JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /Questions/Edit/5

        [HttpPost]
        public ActionResult Edit(Test test)
        {
            try
            {
                TestsModel.Update(test);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }
    }
}
